<?php
declare (strict_types = 1);

namespace app\admin\controller\delivery;

use app\common\controller\Backend;
use app\admin\traits\Actions;
use app\common\model\delivery\Goods;
use think\annotation\route\Group;
use think\annotation\route\Route;
use app\common\model\delivery\Channel as ChannelModel;
use think\facade\Db;

#[Group("delivery/channel")]
class Channel extends Backend
{
    use Actions{
        index as private _index;
        edit as private _edit;
        add as private _add;
        multi as private _multi;
        recyclebin as private _recyclebin;
    }

    protected function _initialize()
    {
        parent::_initialize();
        $this->model = new ChannelModel();
        $this->assign('goods',Goods::where('status','normal')->column('name','id'));
    }

    //查看
    #[Route("GET,JSON","index")]
    public function index()
    {
        return $this->_index();
    }

    //添加
    #[Route("GET,POST","add")]
    public function add()
    {
        if($this->request->isPost()){
            $this->parseData();
            return $this->_add();
        }
        return $this->fetch();
    }

    //修改
    #[Route("GET,POST","edit")]
    public function edit()
    {
        if($this->request->isPost()){
            $this->parseData();
            return $this->_edit();
        }else{
            $ids = $this->request->get('ids');
            $row = Db::name('channel')->where('id',$ids)->find();
            $this->assign('row',$row);
            return $this->fetch();
        }
    }

    private function parseData()
    {
        if($this->request->isPost()){
            $userids=$this->request->post('row.user_id');
            $admin_id=$this->request->post('row.admin_id');
            $id=$this->request->post('row.id');
            if($userids){
                $useridarr=explode(',',$userids);
                foreach ($useridarr as $userid){
                    $channel=ChannelModel::where(function($query) use ($userid,$id){
                        $query->where("FIND_IN_SET({$userid},user_id)");
                        if($id){
                            $query->where('id','<>',$id);
                        }
                    })->find();
                    if($channel){
                        $this->error("微信{$channel->user[0]->nickname}已经绑定了别的业务经理！");
                    }
                }
            }
            if($admin_id){
                $channel=ChannelModel::where(function($query) use ($admin_id,$id){
                    $query->where("admin_id",'=',$admin_id);
                    if($id){
                        $query->where('id','<>',$id);
                    }
                })->find();
                if($channel){
                    $this->error("管理员{$channel->admin->nickname}已经绑定了别的业务经理！");
                }
            }
            $this->postParams['delivery_endtime']='22:00';
        }
    }

    #[Route('POST,GET','multi')]
    public function multi()
    {
        $field = $this->request->param('field');
        $value = $this->request->param('value');
        if($field=='is_default' && $value==1){
            $count=ChannelModel::where(['is_default'=>1])->count();
            if($count>0){
                $this->error('只允许一个自营贸易商！');
            }
        }
        return $this->_multi();
    }

    //回收站
    #[Route("GET,POST,JSON","recyclebin")]
    public function recyclebin($action)
    {
        $this->recyclebinColumns=[
            "title"=>"名称",
            "phone"=>"电话"
        ];
        $this->recyclebinColumnsType=[
            "realname"=>"text",
            "mobile"=>"text",
        ];
        return $this->_recyclebin($action);
    }
}
